package cn.smile.queue;

import cn.smile.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * 监听队列消息
 */
@Component
public class RedisMessageQueueListener implements MessageListener {

    @Autowired
    private RedisTemplate<Integer,String> redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //得到value的序列化方式
        final RedisSerializer<?> serializer = redisTemplate.getValueSerializer();
        //对消息进行序列化
        final Order order = (Order) serializer.deserialize(message.getBody());
        System.out.println(order);
    }

}
